﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace touch
{
    class Program
    {
        static string startdir = null;
        static bool deep = false;
        static DateTime touchTime = DateTime.Now;

        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                List<string> masks = new List<string>();
                for (int pos = 0; pos < args.Length; pos++)//  (string arg in args)
                {
                    string arg = args[pos];
                    if (string.Compare(arg, "/s", true) == 0)
                        deep = true;
                    else if (string.Compare(arg, "/d", true) == 0)
                        startdir = args[++pos];
                    else if (string.Compare(arg, "/t", true) == 0)
                    {
                        string timeStr = args[++pos];
                        touchTime = DateTime.Parse(timeStr);
                    }
                    else if (string.Compare(arg, "/?", true) == 0)
                    {
                        Console.Out.WriteLine("Will change the last write time of the files in the current or specified directory.");
                        Console.Out.WriteLine(" /d - Specify the directory to start touching files in.");
                        Console.Out.WriteLine(" /s - Recurse through child directories to change date.  If not specified only the base directory will have its files touched.");
                        Console.Out.WriteLine(" /t - The specific date/time to change the files to.  If not specified then the current time is used.");
                        Console.Out.WriteLine("Remaining arguments will be treated as filename masks to only touch files that match a given mask.");
                        return;
                    }
                    else
                        masks.Add(arg);
                }

                if(startdir == null)
                    startdir = Directory.GetCurrentDirectory();

                DirectoryInfo di = new DirectoryInfo(startdir);
                if (di.Exists)
                {
                    Console.Out.WriteLine("Will touch files in {0}.", startdir);
                    startdir = di.FullName;
                    processDir(di, masks);
                }
                else
                    Console.WriteLine("Cannot find directory {0}", startdir);
            }
        }

        static void processDir(DirectoryInfo di, List<string> masks)
        {
            if(deep)
                foreach (DirectoryInfo cdi in di.GetDirectories())
                        processDir(cdi, masks);

            if (masks.Count == 0)
                foreach (FileInfo cfi in di.GetFiles())
                    touch(cfi);
            else
                foreach(string mask in masks)
                    foreach (FileInfo cfi in di.GetFiles(mask))
                        touch(cfi);

        }

        static void touch(FileInfo fi)
        {
            fi.LastWriteTime = DateTime.Now;
        }
    }
}
